package at.ac.tuwien.dse.health.event.type;

import com.google.common.base.Objects;

import java.io.Serializable;
import java.util.UUID;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Wraps a certain value of interest for notifying subscribers about a certain happening.
 *
 * @author Bernhard Nickel
 * @author Gregor Schauer
 * @author Dominik Strasser
 */
public class Event<T extends Serializable> implements Serializable {
	UUID id;
	long timestamp;
	T value;
	String source;

	private Event() {
	}

	public Event(T value, String source) {
		this.id = UUID.randomUUID();
		this.timestamp = System.currentTimeMillis();
		this.value = value;
		this.source = checkNotNull(source, "Event source must not be null");
	}

	public UUID getId() {
		return id;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public T getValue() {
		return value;
	}

	@SuppressWarnings("unchecked")
	public Class<T> getType() {
		return (Class<T>) (value != null ? value.getClass() : Object.class);
	}

	public String getSource() {
		return source;
	}

	@Override
	public String toString() {
		return Objects.toStringHelper(this)
				.add("id", id)
				.add("timestamp", timestamp)
				.add("value", value)
				.toString();
	}
}
